package win.zxshuo.www.controller.responses;

import com.fasterxml.jackson.annotation.JsonIgnore;
import win.zxshuo.www.model.BookModel;
import win.zxshuo.www.model.chapters.ChapterModel;
import win.zxshuo.www.service.ChapterService;
import win.zxshuo.www.utils.StringUtils;
import win.zxshuo.www.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BooksResponse extends BaseResponse {


    public BooksResponse(){

    }

    public BooksResponse(List<BookModel> books, ChapterService chapterService){
        List<Map<String,Object>> bookinfos = new ArrayList<>();
        for (BookModel bookModel:books){
            bookinfos.add(setBookToBookinfo(bookModel,chapterService.getFirstChapter(bookModel),chapterService.getLastChapter(bookModel)));
        }
        setData(bookinfos);
        setTrue();
    }

    public void mkBookResponse(List<BookModel> books,ChapterService chapterService){
        List<Map<String,Object>> bookinfos = new ArrayList<>();
        for (BookModel bookModel:books){
            bookinfos.add(setBookToBookinfo(bookModel,chapterService.getFirstChapter(bookModel),chapterService.getLastChapter(bookModel)));
        }
        setData(bookinfos);
        setTrue();
    }

    public BooksResponse(BookModel bookModel,ChapterModel firstChapter,ChapterModel lastChapter){
        setTrue();
        setData(setBookToBookinfo(bookModel,firstChapter,lastChapter));
    }

    private Map<String,Object> setBookToBookinfo(BookModel bookModel,ChapterModel firstChapter,ChapterModel lastChapter){
        Map<String,Object> bookinfo = new HashMap<>();
        bookinfo.put("bookid",bookModel.getId());
        bookinfo.put("name",bookModel.getName());
        bookinfo.put("surface", StringUtils.addUrlProtocol(bookModel.getSurface(),"https"));
        bookinfo.put("status",bookModel.getStatus());
        bookinfo.put("introduction",StringUtils.parsePtoR(bookModel.getIntroduction()));
        bookinfo.put("mark",bookModel.getMark());
        bookinfo.put("category",bookModel.getCategoryModel().getName());
        bookinfo.put("author",bookModel.getAuthorModel().getName());
        bookinfo.put("wordNum",StringUtils.translateIntNumToStr(bookModel.getWordNum()));
        if (firstChapter != null){
            bookinfo.put("firstChapterId",firstChapter.getId());
        }else {
            bookinfo.put("firstChapterId","");
        }
        if (lastChapter != null){
            bookinfo.put("lastChapterName",lastChapter.getTitle());
            bookinfo.put("lastChapterId",lastChapter.getId());
            bookinfo.put("lastChapterUpdate",StringUtils.timeToStr(lastChapter.getModifyDate()));
            bookinfo.put("lastChapterUpdateTime",TimeUtils.compareNowTime(lastChapter.getModifyDate()));
        }else {
            bookinfo.put("lastChapterName","");
            bookinfo.put("lastChapterId","");
            bookinfo.put("lastChapterUpdate","");
            bookinfo.put("lastChapterUpdateTime","");
        }
        return bookinfo;
    }

    public static BooksResponse getBookIndex(List<ChapterModel> chapterModels){
        BooksResponse booksResponse = new BooksResponse();
        booksResponse.setCode(200);
        booksResponse.setData(chapterModels);
        booksResponse.setTrue();
        return booksResponse;
    }



}
